/**
 * 
 */
package com.gko100.action.front;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.gko100.action.base.UserBaseAction;
import com.gko100.model.User;
import com.gko100.service.AdminManager;
import com.gko100.util.DateTimeUtil;
import com.gko100.util.MD5;
import com.gko100.vo.UserVo;
import com.opensymphony.xwork2.ActionContext;

/**
 * @author Administrator
 *
 */
public class UserLoginAction extends UserBaseAction {

	private String result;

	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}

	//User对象和UserVo对象
	private User user;
	private UserVo userVo;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public UserVo getUserVo() {
		return userVo;
	}
	public void setUserVo(UserVo userVo) {
		this.userVo = userVo;
	}
	
	//验证码和错误提示
	private String picValidate;
	private String tip;
	public String getPicValidate() {
		return picValidate;
	}
	public void setPicValidate(String picValidate) {
		this.picValidate = picValidate;
	}
	public String getTip() {
		return tip;
	}
	public void setTip(String tip) {
		this.tip = tip;
	}
	
	
	public String register_user() throws Exception {

		ActionContext ctx = ActionContext.getContext();
		
		ctx.getLocale();
		if(Locale.getDefault().equals(Locale.CHINA)){
			ctx.setLocale(Locale.CHINA);
			
		}else if(Locale.getDefault().equals(Locale.US)){

			ctx.setLocale(Locale.US);
			
		}
		String picValidate2 = (String)ctx.getSession().get("picValidate");
		if (picValidate == null || !picValidate.equals(picValidate2)) {
			
			this.setTip("验证码错误，请重新输入！");
			return INPUT;
		}
		User u =new User();
		u.setName(this.getUserVo().getName());
		if(this.getUserVo().getPassword()!=null){
			u.setPassword(MD5.encoderByMd5(this.getUserVo().getPassword()));
		}
		u.setEmail(this.getUserVo().getEmail());
		u.setAddress(this.getUserVo().getAddress());
		u.setPhone(this.getUserVo().getPhone());
		u.setMobile(this.getUserVo().getMobile());
		u.setSex(this.getUserVo().getSex());
		u.setQq(this.getUserVo().getQq());
		u.setSignDate(DateTimeUtil.getNowTime());
		u.setLogState(null);
		u.setPermState(false);
		
		if(u!=null){
			userManager.add_user(u);
		}
		return "user_register_success";
	}
	
	public String validate_userLogin() throws Exception{
		
		ActionContext ctx = ActionContext.getContext();
		
		ctx.getLocale();
		if(Locale.getDefault().equals(Locale.CHINA)){
			ctx.setLocale(Locale.CHINA);
			
		}else if(Locale.getDefault().equals(Locale.US)){

			ctx.setLocale(Locale.US);
			
		}
		if(this.getTip()!=null){
			this.setTip(null);
		}
		
		String picValidate2 = (String)ctx.getSession().get("picValidate");
		if (picValidate == null || !picValidate.equals(picValidate2)) {
			this.setTip("验证码不正确，请重新输入");
			return INPUT;
		}
		
		String pass = MD5.encoderByMd5(this.getUserVo().getPassword());
		User u = userManager.findUserByNameAndPassword(this.getUserVo().getName(), pass);
		if(u!=null){
			
			boolean flag = userManager.validLogin(u);
			if(flag){
				
				ctx.getSession().put("user", u);
				setTip("已成功登录，转向后台管理页面...");
				return "userLogin_validate_success";
			}else{
				
				setTip("用户名或密码错误，请重新输入！");
				return INPUT;
			}
		}else{
			
			setTip("用户名或密码错误，请重新输入！");
			return INPUT;
		}
	}
	
	public String checkUsername() throws Exception{
		
		Map<String, String> map = new HashMap<String, String>();   
        
		ActionContext ctx = ActionContext.getContext();
		
		ctx.getLocale();
		if(Locale.getDefault().equals(Locale.CHINA)){
			ctx.setLocale(Locale.CHINA);
			
		}else if(Locale.getDefault().equals(Locale.US)){

			ctx.setLocale(Locale.US);
			
		}
        HttpServletRequest req = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);   
        HttpServletResponse res = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);   
        
        if(this.getUserVo().getName().trim()!=null&&!this.getUserVo().getName().equals("")){
        	
        	String userName = this.getUserVo().getName();
            map.put("userName", userName);   
            res.setContentType("text/html;charset=UTF-8");   
            
            PrintWriter w = res.getWriter();   
            User u = userManager.findUserByName(userName);
            if(u!=null){   
                
                JSONObject jo = JSONObject.fromObject(map);      
                this.setResult(jo.toString());  
                String tip="用户名已存在,请重新输入....";
                w.println(tip);   
            }else{   
            	String tip="可以使用....";
            	w.println(tip);  
            }   
            w.close();
           
        	
        }else{
        	   
            res.setContentType("text/html;charset=UTF-8");
        	PrintWriter w = res.getWriter();
        	String tip = "不能为空，请重新输入!";
        	w.println(tip);
        	w.close();
        	
        }
        
        return "usernameCheck_success";
		
		
	}
	public String logout_user() throws Exception {
		
		
		
		
		return "user_logout_success";
	}
	public String find_user() throws Exception {
		
		ActionContext ctx = ActionContext.getContext();
		User user = (User)ctx.getSession().get("user");
		if(user!=null){
			
			this.setUser(user);
			
		}
		
		return "user_find_success";
	}
	public String find_userHealth() throws Exception{
		
		
		return "userHealth_find_success";
	}
	
	
	
	
	
	
	
	
	
	
	
}
